<template>
  <div class="common-layout__commentI">
    <div class="header">
      <div>
        <el-button @click="deleteBatch(multipleSelection)">删除弹幕</el-button>
        <el-button @click="getComment" :icon="Refresh">刷新</el-button>
      </div>
      <div>
        <el-select
          v-model="selectedVideoId"
          class="video-select"
          @change="changeVideo"
        >
          <el-option
            v-for="item in videoOptions"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            >{{ item.title }}
          </el-option>
        </el-select>
      </div>
    </div>
    <div class="main__comment">
      <el-table
        ref="multipleTableRef"
        :data="commentTableData.records"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        stripe
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="发送者" width="120" show-overflow-tooltip>
          <template #default="scope">
            {{ userName(scope.row.user.name, scope.row.user.id) }}
          </template>
        </el-table-column>

        <el-table-column
          v-if="selectedVideoId == '0'"
          property="video.title"
          label="视频标题"
          width="160"
          show-overflow-tooltip
        >
          <template #default="scope">
            <RouterLink class="link" :to="`/video/${scope.row.video.id}`">{{ scope.row.video.title }}</RouterLink>
          </template>
        </el-table-column>
        <el-table-column
          property="content"
          label="评论内容"
          width="380"
          show-overflow-tooltip
        />
        <el-table-column
          property="createTime"
          label="发送时间"
          width="180"
          show-overflow-tooltip
        />
        <el-table-column label="删除" width="100">
          <template #default="scope">
            <el-button
              size="small"
              type="danger"
              @click="deleteBatch([scope.row])"
              >Delete</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="page.pageIndex"
        :page-size="page.pageSize"
        @current-change="getComment"
        background
        layout="prev, pager, next"
        :total="commentTableData.total"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { Refresh } from "@element-plus/icons-vue";
import { ref, onMounted, onUnmounted } from "vue";
import { Video } from "@/types/video";
import { PageWithVideoId, Comment } from "@/types/comment";
import commentApi from "@/api/comment";
import videoApi from "@/api/video";
import { successNotify, timeConversion, userName } from "@/utils/common";
import { PageResp } from "@/types/page";

const videoOptions = ref(<Video[]>[]);
const page = ref(<PageWithVideoId>{
  pageIndex: 0,
  pageSize: 20,
});
const selectedVideoId = ref<string>("0");
const multipleSelection = ref<Comment[]>([]);
const commentTableData = ref(<PageResp<Comment>>{});

// 获取自己的视频标题
const getVideosTitle = async () => {
  const { code, data } = await videoApi.getIdTitle();
  if (code === 0) {
    videoOptions.value.push(...data);
  }
};

const handleSelectionChange = (val: any) => {
  multipleSelection.value = val;
};

const changeVideo = () => {
  page.value.pageIndex = 1;
  getComment();
};

const getComment = async () => {
  if (selectedVideoId.value === "0") {
    delete page.value.videoId;
  } else {
    page.value.videoId = selectedVideoId.value;
  }
  console.log(page.value);

  const { code, data } = await commentApi.page(page.value);
  if (code === 0) {
    commentTableData.value = data;
  }
};

const deleteBatch = async (selected: Comment[]) => {
  if (selected.length === 0) return;
  ElMessageBox.confirm("确认删除所选评论吗？")
    .then(async () => {
      let ids = selected.map((v) => v.id);
      const { code, data } = await commentApi.remove(ids);
      if (code === 0) {
        successNotify(data);
        getComment();
      }
    })
    .catch(() => {});
};

onMounted(() => {
  videoOptions.value.push(<Video>{ id: "0", title: "全部视频" });
  getVideosTitle();
  getComment();
});
</script>

<style lang="scss" scoped>
.common-layout__commentI {
  & > .header {
    display: flex;
    justify-content: space-between;
  }
  & > .pagination {
    margin-top: 20px;
  }
}
</style>

<style lang="scss"></style>
